#include <camera.h>
#include <QtMath>


QMatrix4x4 Camera::getViewMat()
{
    QMatrix4x4 view;
    view.translate({cameraPos.x(),cameraPos.y(),0}); // 窗口中心
    view.rotate(pitch, {1,0,0});
    view.rotate(yaw, 0,1,0); // 进行旋转
    view.translate(-cameraCenter); // 移到中心点
    return view;
}


void Camera::DealKeyInput(CameraMovement direction){
    GLfloat velocity = movementSpeed;
    QVector3D dir = {0,0,0};
    switch(direction) {
    case LEFT:
        dir = {-1,0,0};
        break;
    case RIGHT:
        dir = {1, 0,0};
        break;
    case UP:
        dir = {0,1,0};
        break;
    case DOWN:
        dir = {0,-1,0};
        break;
    default:
        break;
    }

    cameraPos += dir *velocity;
    // qDebug()<<dir<<endl;
}

void Camera::DealMouseScroll(GLfloat offset) {
    zoom-=offset;
    if(zoom<0.001f)zoom=0.001f;
    if(zoom>1000.0f)zoom=1000.0f;
}

void Camera::DealMouseMovement(GLfloat Xoffset, GLfloat Yoffset){
    yaw+=Xoffset*sensitivity;
    pitch+=Yoffset*sensitivity;
    // qDebug()<<yaw<<pitch;

    // qDebug()<<getViewMat();
}

void Camera::DealKeyboardInputs() {
    if(keys[Qt::Key_W])DealKeyInput(UP);
    if(keys[Qt::Key_S])DealKeyInput(DOWN);
    if(keys[Qt::Key_A])DealKeyInput(LEFT);
    if(keys[Qt::Key_D])DealKeyInput(RIGHT);
}
